//
//  LoginViewController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/4.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginByMobileView.h"
#import "LoginByAccountView.h"
#import "AppDelegate.h"
#import "MainTabBarController.h"
#import "BaseNavgationController.h"
#import "I_APPManager.h"
#import "CompletionAccountInfoController.h"


@interface LoginViewController ()
@property (nonatomic, strong) LoginByMobileView *loginByMobileView;
@property (nonatomic, strong) LoginByAccountView *loginByAccountView;
@property (nonatomic, strong) UIView *loginView;
@property (nonatomic, assign) LoginWayType loginWayType;;

@end

@implementation LoginViewController


#pragma mark Getter/Setter
- (LoginByMobileView *)loginByMobileView
{
    if (_loginByMobileView == nil) {
        _loginByMobileView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LoginByMobileView class]) owner:nil options:nil] lastObject];
        WeakSelf(self);
        _loginByMobileView.changeToAccountBlock = ^{
            weakself.loginWayType = LoginWayTypeByAccount;
        };
        _loginByMobileView.loginSuccessBlock = ^(M_Account *account) {
            [weakself loginSuccessWithAccount:account];
        };
        _loginByMobileView.loginErrorBlock = ^(NSError *error) {
            [weakself loginError:error];
        };
        
    }
    return _loginByMobileView;
}

- (LoginByAccountView *)loginByAccountView
{
    if (_loginByAccountView == nil) {
        _loginByAccountView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LoginByAccountView class]) owner:nil options:nil] lastObject];
        WeakSelf(self);
        _loginByAccountView.changeToMobileBlock = ^{
            weakself.loginWayType = LoginWayTypeByMobile;
        };
        _loginByAccountView.loginSuccessBlock = ^(M_Account *account) {
            [weakself loginSuccessWithAccount:account];
        };
        _loginByAccountView.loginErrorBlock = ^(NSError *error) {
            [weakself loginError:error];
        };
        
    }
    return _loginByAccountView;
}

- (void)setLoginWayType:(LoginWayType)loginWayType
{
    _loginWayType = loginWayType;
    UIView *toView;
    switch (loginWayType) {
        case LoginWayTypeByMobile:
        {
            toView = self.loginByMobileView;
        }
            break;
        case LoginWayTypeByAccount:
        {
            toView = self.loginByAccountView;
        }
            break;
            
        default:
            break;
    }
    if (_loginView) {
        toView.frame = CGRectMake(0, 0, self.view.width, 224);
        toView.center = self.view.center;
        toView.left = self.view.width;
        [self.view addSubview:toView];
        [UIView animateWithDuration:0.35 animations:^{
            self.loginView.right = 0;
            toView.left = 0;
        } completion:^(BOOL finished) {
            [self.loginView removeFromSuperview];
            self.loginView = toView;
        }];
    } else {
        toView.frame = CGRectMake(0, 0, self.view.width, 224);
        toView.center = self.view.center;
        [self.view addSubview:toView];
        _loginView = toView;
    }
    
}




#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    
    UITapGestureRecognizer *hideKeyBoardTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    [self.view addGestureRecognizer:hideKeyBoardTap];
    
    self.loginWayType = LoginWayTypeByMobile;

}

#pragma mark Config/UI
- (void)configUI{
    

}

- (void)loginSuccessWithAccount:(M_Account *)account
{
    RI.account = account;
    [I_APPManager upLoadPushToken];
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (account.brandName) {
        MainTabBarController *mainTabBarVC = [[MainTabBarController alloc] init];
        delegate.window.rootViewController = mainTabBarVC;
    } else {
        CompletionAccountInfoController *vc = [CompletionAccountInfoController controller];
        BaseNavgationController *navVC = [[BaseNavgationController alloc] initWithRootViewController:vc];
        delegate.window.rootViewController = navVC;
    }
    
}

- (void)loginError:(NSError *)error
{
    if (error.code == 3003 || error.code == 3012) {//账号禁用
        [AV hideen];
        [YDAlertViewController alertViewControllerWithMessage:@"当前账号已过期，请前往公众号【云动助手】进行开通高级版服务" cancleTitle:@"取消" confimTitle:@"复制公众号名称" cancleBlock:nil confimBlock:^{
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = @"云动助手";
            [AV showSuccess:@"公众号【云动助手】已添加到剪切板"];
        }];
    } else {
        [AV showError:error];
    }
}

#pragma mark Action

- (IBAction)userAgreementVC:(id)sender {
    YDWebViewController *webVC = [YDWebViewController controllerWithUrl:Url_ForUserAgreement];
    [self.navigationController pushViewController:webVC animated:YES];
}
- (IBAction)userPrivacyBtnClicked:(id)sender {
    YDWebViewController *webVC = [YDWebViewController controllerWithUrl:Url_ForUserPravicy];
    [self.navigationController pushViewController:webVC animated:YES];
}



- (void)hideKeyBoard
{
    [self.view endEditing:YES];
}
@end
